package com.hgyc.mom.wms.controller;

import java.util.*;

import com.hgyc.mom.wms.vo.CrmSalesOrderVO;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.wms.entity.CrmSalesOrder;
import com.hgyc.mom.wms.vo.CrmSalesOrderParam;
import com.hgyc.mom.wms.service.CrmSalesOrderService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 销售订单Controller
 *
 * @author fwj
 * @since 2025-05-19
 */
@Tag(name = "销售订单控制器", description = "销售订单控制器")
@RestController
@RequestMapping("/wms/saleOrder")
public class CrmSalesOrderController {
    @Autowired
    private CrmSalesOrderService crmSalesOrderService;

    @Operation(summary = "查询销售订单列表", description = "查询销售订单列表")
    @PostMapping("/pageList")
    public Result<IPage<CrmSalesOrder>> pageList(@Parameter(description = "查询参数") @RequestBody CrmSalesOrderParam crmSalesOrderParam) {
        Page<CrmSalesOrder> page = new Page<>(crmSalesOrderParam.getPage(), crmSalesOrderParam.getPageSize());
        CrmSalesOrder crmSalesOrder = new CrmSalesOrder();
        BeanUtils.copyProperties(crmSalesOrderParam, crmSalesOrder);
        QueryWrapper<CrmSalesOrder> wrapper = QueryWrapperUtil.build(crmSalesOrder);
        IPage<CrmSalesOrder> list = crmSalesOrderService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取销售订单详细信息", description = "获取销售订单详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "销售订单ID") @PathVariable("id") Long id) {
        return Result.success(crmSalesOrderService.getById(id));
    }
    @Operation(summary = "新增销售订单", description = "新增销售订单")
    @PostMapping
    public Result save(@Parameter(description = "销售订单") @RequestBody CrmSalesOrderVO crmSalesOrderVO) {
        crmSalesOrderService.saveSaleOrder(crmSalesOrderVO);
        return Result.success();
    }

    @Operation(summary = "修改销售订单", description = "修改销售订单")
    @PutMapping
    public Result update(@Parameter(description = "销售订单") @RequestBody CrmSalesOrderVO crmSalesOrderVO) {
        crmSalesOrderService.updateSaleOrder(crmSalesOrderVO);
        return Result.success();
    }

    @Operation(summary = "删除销售订单", description = "删除销售订单")
    @DeleteMapping
    public Result remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        crmSalesOrderService.removeSaleOrder(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<CrmSalesOrder> util = new ExcelUtil<>(CrmSalesOrder.class);
        util.importTemplateExcel(response, "销售订单");
    }

    @Operation(summary = "导出销售订单列表", description = "导出销售订单列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmSalesOrderParam crmSalesOrderParam) {
        CrmSalesOrder crmSalesOrder = new CrmSalesOrder();
        BeanUtils.copyProperties(crmSalesOrderParam, crmSalesOrder);
        QueryWrapper<CrmSalesOrder> wrapper = QueryWrapperUtil.build(crmSalesOrder);
        List<CrmSalesOrder> list = crmSalesOrderService.list(wrapper);
        ExcelUtil<CrmSalesOrder> util = new ExcelUtil<>(CrmSalesOrder.class);
        util.exportExcel(response, list, "销售订单数据");
    }

    @Operation(summary = "导入销售订单列表", description = "导入销售订单列表")
    @PostMapping("/import")
    public Result importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<CrmSalesOrder> util = new ExcelUtil<>(CrmSalesOrder.class);
        List<CrmSalesOrder> crmSalesOrderList = util.importExcel(file.getInputStream());
        String message = crmSalesOrderService.importData(crmSalesOrderList, updateSupport);
        return Result.success(message);
    }
}
